<template>
  <div></div>
</template>

<script>
/*eslint-disable*/
import pubsub from "pubsub-js";
import { getCurrentInstance, onMounted } from "vue";
import axios from "axios";

import urlList from "@/store/urlList.json";
import normalApi from "@/api/normalApi.js";
import Message from "@/components/Message/showMessage";

export default {
  name: "多边形空间查询",
  data() {
    return {
      subList: [],
    };
  },
  setup() {
    const { proxy } = getCurrentInstance();

    // 查询URL
    // let serveURLList;
    // normalApi.getUrlList().then((res) => {
    //   serveURLList = [
    //     res.urlList.addressURL,
    //     res.urlList.courtYardURL,
    //     res.urlList.naturalResourceURL,
    //     res.urlList.roadUrl,
    //     res.urlList.enterpriseURL,
    //   ];
    // });

    let serveURLList = [
      urlList.addressURL,
      urlList.courtYardURL,
      urlList.naturalResourceURL,
      urlList.roadUrl,
      urlList.enterpriseURL,
    ];

    // 查询图层名
    let serveNameList = ["地址", "院落", "自然资源", "道路", "企业"];

    onMounted(() => {
      subscribeList();
    });

    function subscribeList() {
      proxy.subList.push(
        // 接收查询多边形点集
        pubsub.subscribe("polygonQueryRun", (n, data) => {
          console.log(data);
          polygonQueryRun(data);
        })
      );
    }

    // 执行查询
    async function polygonQueryRun(polygon) {
      // 制作请求
      let requestList = await makeRequest(polygon);
      // 发送请求
      Promise.all(requestList)
        .then((response) => {
          // console.log(response);
          let resultList = [];
          let resultListLength = 0;
          for (let index = 0; index < response.length; index++) {
            // console.log(response[index]);
            // 数据集
            let list = response[index].data.features;
            resultListLength += list.length;
            // 数据集名称
            let title = serveNameList[index];
            // 数据类型
            let type = response[index].data.geometryType;
            // 数据录入
            resultList.push({ list, title, type });
          }
          // 信息提示
          Message(
            `查询${
              resultListLength > 0
                ? `成功，共查询到${resultListLength}条数据`
                : `成功，查询范围内无数据`
            }`
          );
          // 结果处理
          requestDataHandle(resultList);
        })
        .catch((error) => {
          Message(`查询失败：${error}`);
        });
    }

    // 发送查询请求
    function makeRequest(polygon) {
      // 查询点集转置整理
      let pointList = [];
      polygon.forEach((element) => {
        pointList.push([element.lng, element.lat]);
      });
      // 查询请求参数准备
      let requestList = [];
      serveURLList.forEach((url) => {
        // 设置查询参数
        const params = {
          geometry: { rings: [pointList] },
          f: "json", // 返回JSON格式数据
          geometryType: "esriGeometryPolygon",
          spatialRel: "esriSpatialRelIntersects", // 空间关系为相交查询
          outFields: "*", // 返回所有字段
          returnGeometry: true, // 返回几何信息
          mapExtent: "45330.913495,171563.757422,80595.887643,203928.9151",
          imageDisplay: "60055096",
        };

        requestList.push(axios.get(url, { params }));
      });

      // 发送 GET 请求
      return requestList;
    }

    // 查询结果处理
    function requestDataHandle(resultList) {
      // console.log(resultList);
      // 查询结果列表显示
      pubsub.publish("queryInfoShow", {
        // 显隐标识
        flag: true,
        // 数据集
        resultList,
      });
    }
  },
  beforeDestroy() {
    this.subList.forEach((element) => {
      pubsub.unsubscribe(element);
    });
  },
  beforeRouteLeave() {
    this.subList.forEach((element) => {
      pubsub.unsubscribe(element);
    });
  },
};
</script>

<style>
</style>